package com.shangxia.lucky.lucky.job;

import com.shangxia.lucky.lucky.entity.Model;
import com.shangxia.lucky.lucky.job.write.MatchColumnWriterThread;
import com.shangxia.lucky.lucky.job.write.OverWriterThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import static java.util.stream.Collectors.toList;

/**
 * @author zhouya38158
 * @Package com.shangxia.lucky.lucky.job
 * @date 2021/11/16 10:56
 */
@Slf4j
@Component
public class MatchColumnJob extends MatchJob {

    @Override
    public void job() throws IOException {
        OverWriterThread overWriterThread = new OverWriterThread(configText);

        //开始准备输出翻层结果
        new Thread(overWriterThread).start();

        //最后一行 初始化
        initLAST_LIST(configText.getBananaExportColumn());

        List<List<Integer>> matchLists = readCode(configText, configText.getBananaMatchFileName());

        MatchColumnWriterThread matchColumnWriterThread = new MatchColumnWriterThread(configText, matchLists.size());

        new Thread(matchColumnWriterThread).start();

        Map<String, Object> params = new HashMap<>();
        List<List<Integer>> lists = readCode(configText, configText.getBananaRealCodeFileName());
        params.put("matchLists", matchLists);
        turnOver(params, lists, configText);

        matchColumnWriterThread.stop();
        overWriterThread.stop();

        logOver();

    }

    @Override
    public void doPrivateJob(Map<String, Object> params, List<List<Integer>> realCodeLists) {
        List<List<Integer>> matchLists = (List<List<Integer>>) params.get("matchLists");
        List<Integer> columnList = (List<Integer>) params.get("columnList");
        Integer row = (Integer) params.get("row");
        Integer column = (Integer) params.get("column");

        for (int type = 0; type < matchLists.size(); type++) {
            if (!matchLists.get(type).get(0).equals(column + 1)) {
                continue;
            }
            Set<Integer> set = new HashSet<>(matchLists.get(type).subList(1, matchLists.get(type).size()));
            set.addAll(columnList.subList(configText.getBananaCodeLength() - configText.getBananaSonCodeLength(), columnList.size()));
            //匹配个数
            int matchCount = configText.getBananaSonCodeLength() + matchLists.get(type).size() - set.size();
            Model model = new Model();
            model.setRow(row);
            model.setColumn(column);
            model.setType(type);
            model.setMatchCount(matchCount);
            if (row.equals(column)) {
                model.setAge(1);
                model.setMatchCountAll(matchCount);
            } else {
                Model lastModel = ROW_LAST_MODELS.get(column + "&" + type);
                if (configText.getBananaMatchColumnMatchSize() > 0) {
                    if (matchCount == configText.getBananaMatchColumnMatchSize()) {
                        model.setAge(0);
                    } else {
                        model.setAge(lastModel.getAge() + 1);
                    }
                } else {
                    if (lastModel.getPrized()) {
                        model.setAge(0);
                    } else {
                        model.setAge(lastModel.getAge() + 1);
                    }
                }
                model.setMatchCountAll(lastModel.getMatchCountAll() + matchCount);
            }
            if (configText.getBananaMatchColumnMatchSize() > 0) {
                if (matchCount == configText.getBananaMatchColumnMatchSize()) {
                    model.setPrized(true);
                } else {
                    model.setPrized(false);
                }
            } else {
                model.setPrized(matchCount == Math.min(matchLists.get(type).size(), configText.getBananaSonCodeLength()));
            }
            ROW_MODELS.put(column + "&" + type, model);
        }
    }

    @Override
    public void exportRowResult(Map<String, Object> params) {
        List<Model> resultList = ROW_MODELS.values().stream()
                .sorted(Comparator.comparingInt(Model::getType))
                .sorted(Comparator.comparingInt(Model::getColumn))
                .collect(toList());
        lastRowData(resultList);
        List<String> columnList = new ArrayList<>();
        columnList.add(1 + (int) params.get("row") + "");
        resultList.stream().forEach(model -> {
            columnList.add("" + (1 + model.getColumn()));
            columnList.add("" + (1 + model.getType()));
            columnList.add("" + model.getAge());
            columnList.add("" + model.getMatchCount());
            columnList.add("" + model.getPrized());
        });
        MatchColumnWriterThread.columnQueue.add(getBigList(columnList));
    }

    /**
     * 计算最后一行数据
     *
     * @param resultList
     */
    @Override
    public void lastRowData(List<Model> resultList) {

    }

}
